package com.three.service.impl;

import com.baor.baseUtil.R;
import com.three.dao.UserDao;
import com.three.domain.Classes;
import com.three.domain.Teacher;
import com.three.domain.Users;
import com.three.domain.role;
import com.three.service.UserService;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Override
    public R userList(String keyword, Integer pageIndex, Integer pageSize){
        pageIndex = (pageIndex - 1) * pageSize;
        double pageCount = userDao.PageCount(keyword);
        pageCount = Math.ceil(pageCount/ pageSize);
        System.out.println(pageCount);
        List<Teacher> userList = userDao.userList(keyword, pageIndex, pageSize);
        Map<String, Object> map = new HashMap<>();
        map.put("pageCount", pageCount);
        map.put("userList", userList);
        return R.ok(map);
    }

    @Override
    public R addUser(Users users) {
        String plainPassword = users.getPassword();
        String hashedPassword = BCrypt.hashpw(plainPassword, BCrypt.gensalt());
        users.setPassword(hashedPassword);

        return R.ok(userDao.addUser(users));
    }

    @Override
    public R deleteUser(List<Integer> teacher_ids) {
        return R.ok(userDao.deleteUser(teacher_ids));
    }

    @Override
    public R updateUser(Users users) {
        String plainPassword = users.getPassword();
        String hashedPassword = BCrypt.hashpw(plainPassword, BCrypt.gensalt());
        users.setPassword(hashedPassword);

        return R.ok(userDao.updateUser(users));
    }

    @Override
    public R roleList() {
        return R.ok(userDao.roleList());
    }
}
